package com.cnyao.phot;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.provider.BaseColumns;

/*
 * The class UserDatabase is work to get the SQLite access things.
 * */
public class UserDatabase extends SQLiteOpenHelper implements BaseColumns {
	
/*	private static final String DATABASE_NAME="flicker";
	private static final int DATABASE_VERSION=1;
	static final String TABLE_USERS="users";
	static final String COLUMN_USERNAME="username";
	static final String COLUMN_REALNAME="realname";
	static final String COLUMN_NSID="nsid";
	static final String COLUMN_BUDDY_ICON="buddy_icon";
	static final String COLUMN_LAST_UPDATE="last_update";
	static final String SORT_DEFAULT=COLUMN_USERNAME+" ASC";*/
	
	private Context mContext;

	public UserDatabase(Context context) {
		super(context, "ficker", null, 1);
		mContext=context;
	}

	@Override
	/*
	 * Question:
	 *   what would do when the table is already existed?
	 *   Called when the database is created for the first time. 
	 *   This is where the creation of tables and the initial 
	 *   population of the tables should happen.
	 * */
	public void onCreate(SQLiteDatabase db) {
		db.execSQL("CREATE TABLE users(_id INTEGER" +
				"PRIMARY KEY, username TEXT, realname " +
				"TEXT, nsid TEXT, buddy_icon BLOB," +
				"last_update INTEGER);");
		addUser(db,"Bob Lee","Bob Lee","45701389@N00", R.drawable.ubuntu);
		addUser(db,"ericktseng", "Erick Tseng", "76701017@N00", R.drawable.debian);
		addUser(db,"romainguy", "Romain Guy", "24046097@N00", R.drawable.bugs);
	}

	@Override
	/*
	 * Called when the database needs to be upgraded. 
	 * The implementation should use this method to 
	 * drop tables, add tables, or do anything else 
	 * it needs to upgrade to the new schema version. 
	 * */
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS users");
		onCreate(db);
	}

	private void addUser(SQLiteDatabase db, String userName, String realName,
			String nsid, int icon){
		final ContentValues values=new ContentValues();
		values.put("username", userName);
		values.put("realname", realName);
		values.put("nsid", nsid);
		values.put("last_update", System.currentTimeMillis());
		
		final Bitmap bitmap=BitmapFactory.decodeResource(mContext.getResources(),
				icon);
		writeBitmap(values,"buddy_icon",bitmap);
		db.insert("users", "last_update", values);
	}
	
	static void writeBitmap(ContentValues values, String name, Bitmap bitmap){
		if(bitmap!=null){
			//why *2?
			int size=bitmap.getWidth()*bitmap.getHeight()*2;
			ByteArrayOutputStream out=new ByteArrayOutputStream(size);
			try{
				bitmap.compress(Bitmap.CompressFormat.PNG, 
						100, out);
				out.flush();
				out.close();
				
				//put the images into the database.
				values.put(name, out.toByteArray());
			}
			catch(IOException e){
				
			}
		}
	}
}
